package user

import (
	"encoding/json"
	"net/url"

	"gitee.com/suguo79/xyh"
)

// 1.获取当前用户登录信息
func UserGet(ticket *string) (*RespLoginInfo, error) {
	if ticket == nil {
		return nil, nil
	}

	params := url.Values{}
	params.Add("user_ticket", *ticket)

	payload, err := xyh.HttpRequest("cgi-bin/v2/userget", params, nil)
	if err != nil {
		return nil, err
	}

	data := &RespLoginInfo{}
	if err := json.Unmarshal(*payload, &data); err != nil {
		return nil, err
	}
	return data, nil
}

// 2.获取老师用户信息
func UserInfo(orgid, userid, openid *string) (*RespTeacherInfo, error) {
	if orgid == nil {
		return nil, nil
	}

	path := "cgi-bin/user/info"
	params := url.Values{}
	params.Add("org_id", *orgid)
	if userid != nil {
		params.Add("user_id", *userid)
		path = "cgi-bin/userinfo/get"
	}
	if openid != nil {
		params.Add("open_id", *openid)
		path = "cgi-bin/user/info"
	}

	payload, err := xyh.HttpRequest(path, params, nil)
	if err != nil {
		return nil, err
	}

	data := &RespTeacherInfo{}
	if err := json.Unmarshal(*payload, &data); err != nil {
		return nil, err
	}
	return data, nil
}

// 3.获取家长孩子信息
func UserChildren(orgid, openid *string) (*RespChildInfo, error) {
	if orgid == nil || openid == nil {
		return nil, nil
	}

	params := url.Values{}
	params.Add("org_id", *orgid)
	params.Add("open_id", *openid)

	payload, err := xyh.HttpRequest("cgi-bin/user/children", params, nil)
	if err != nil {
		return nil, err
	}

	data := &RespChildInfo{}
	if err := json.Unmarshal(*payload, &data); err != nil {
		return nil, err
	}
	return data, nil
}

// 4.获取学生用户信息
func StudentInfo(orgid, userid *string) (*RespStudentInfo, error) {
	if orgid == nil || userid == nil {
		return nil, nil
	}

	params := url.Values{}
	params.Add("org_id", *orgid)
	params.Add("user_id", *userid)

	payload, err := xyh.HttpRequest("cgi-bin/student/info", params, nil)
	if err != nil {
		return nil, err
	}

	data := &RespStudentInfo{}
	if err := json.Unmarshal(*payload, &data); err != nil {
		return nil, err
	}
	return data, nil
}

// 5.获取部门下人员
func DeptUserlist(orgid, deptid *string) {

}

// 6. 获取用户所在部门
func UserDeptlist(orgid, userid *string) {

}

// 7. 获取部门下岗位成员
func DeptstationUserlist(orgid, deptid *string) {

}

// 8.获取用户的岗位
func UserStation(orgid, userid *string) {

}

// 9.获取老师任教科目
func UserSubject(orgid, userid *string) {

}
